package com.example.wangfanwangluo.wangfanboss.Adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.wangfanwangluo.wangfanboss.Models.PeizaiyundanModel;
import com.example.wangfanwangluo.wangfanboss.Models.PeizaizhongzhuanModel;
import com.example.wangfanwangluo.wangfanboss.R;

import java.util.List;

/**
 * Created by wanghouyi on 2017/11/29.
 */

public class PeizaiyundanAdapter extends BaseAdapter {


    private Context context;
    private List<Object> list;
    public PeizaiyundanAdapter(Context context,List<Object> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public int getItemViewType(int position) {
        int result = 0;
        if (list.get(position) instanceof PeizaiyundanModel) {
            result = 0;
        } else if (list.get(position) instanceof PeizaizhongzhuanModel) {
            result = 1;
        }
        return result;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        View view;
        ViewHolder1 viewHolder1;
        ViewHolder2 viewHolder2;
        int type = getItemViewType(position);

        if (convertView == null) {
            viewHolder1 = new ViewHolder1();
            viewHolder2 = new ViewHolder2();

            if (type == 0) {
                view = LayoutInflater.from(context).inflate(R.layout.adapter_peizai, parent, false);
                viewHolder1.goodsID = (TextView)view.findViewById(R.id.peizaiyundan_goods_id);
                viewHolder1.fromPlace = (TextView)view.findViewById(R.id.peizaiyundan_place_from);
                viewHolder1.toPlace = (TextView)view.findViewById(R.id.peizaiyundan_place_to);
                viewHolder1.fromName = (TextView)view.findViewById(R.id.peizaiyundan_name_from);
                viewHolder1.toName = (TextView)view.findViewById(R.id.peizaiyundan_name_to);
                viewHolder1.goodsType = (TextView)view.findViewById(R.id.peizaiyundan_goods_type);
                viewHolder1.time = (TextView)view.findViewById(R.id.peizaiyundan_time);
                viewHolder1.status = (TextView)view.findViewById(R.id.peizaiyundan_status);
                view.setTag(R.id.tag_peizaiyundan_peizai, viewHolder1);
            }else {
                view = LayoutInflater.from(context).inflate(R.layout.adapter_zhongzhuan, parent, false);
                viewHolder2.goodsID = (TextView)view.findViewById(R.id.peizaiyuanyundan_goods_id);
                viewHolder2.fromPlace = (TextView)view.findViewById(R.id.peizaiyuanyundan_place_from);
                viewHolder2.toPlace = (TextView)view.findViewById(R.id.peizaiyuanyundan_place_to);
                viewHolder2.fromName = (TextView)view.findViewById(R.id.peizaiyuanyundan_name_from);
                viewHolder2.toName = (TextView)view.findViewById(R.id.peizaiyuanyundan_name_to);
                viewHolder2.goodsType = (TextView)view.findViewById(R.id.peizaiyuanyundan_goods_type);
                viewHolder2.time = (TextView)view.findViewById(R.id.peizaiyuanyundan_time);
                viewHolder2.status = (TextView)view.findViewById(R.id.peizaiyuanyundan_status);

                viewHolder2.goodsID2 = (TextView)view.findViewById(R.id.peizaizhongzhuanyundan_goods_id);
                viewHolder2.fromPlace2 = (TextView)view.findViewById(R.id.peizaizhongzhuanyundan_place_from);
                viewHolder2.toPlace2 = (TextView)view.findViewById(R.id.peizaizhongzhuanyundan_place_to);
                viewHolder2.fromName2 = (TextView)view.findViewById(R.id.peizaizhongzhuanyundan_name_from);
                viewHolder2.toName2 = (TextView)view.findViewById(R.id.peizaizhongzhuanyundan_name_to);
                viewHolder2.goodsType2 = (TextView)view.findViewById(R.id.peizaizhongzhuanyundan_goods_type);
                viewHolder2.time2 = (TextView)view.findViewById(R.id.peizaizhongzhuanyundan_time);
                viewHolder2.status2 = (TextView)view.findViewById(R.id.peizaizhongzhuanyundan_status);
                view.setTag(R.id.tag_peizaiyundan_zhongzhuan, viewHolder2);
            }
        }else {
            view = convertView;
            if (type == 0) {
                viewHolder1 = (ViewHolder1) convertView.getTag(R.id.tag_peizaiyundan_peizai);
                viewHolder2 = new ViewHolder2();
            }else {
                viewHolder2 = (ViewHolder2) convertView.getTag(R.id.tag_peizaiyundan_zhongzhuan);
                viewHolder1 = new ViewHolder1();
            }

        }

        Object obj = list.get(position);

        if (type == 0) {
            PeizaiyundanModel model = (PeizaiyundanModel)obj;
            viewHolder1.goodsID.setText(model.getGoodsID());
            viewHolder1.fromPlace.setText(model.getFromPlace());
            viewHolder1.toPlace.setText(model.getToPlace());
            viewHolder1.fromName.setText("("+model.getFromName()+")");
            viewHolder1.toName.setText("("+model.getToName()+")");
            viewHolder1.goodsType.setText(model.getGoodsType());
            viewHolder1.time.setText(model.getTime());
            viewHolder1.status.setText("【"+model.getStatus()+"】");
        }else {
            PeizaizhongzhuanModel model = (PeizaizhongzhuanModel) obj;
            viewHolder2.goodsID.setText(model.getGoodsID());
            viewHolder2.fromPlace.setText(model.getFromPlace());
            viewHolder2.toPlace.setText(model.getToPlace());
            viewHolder2.fromName.setText("("+model.getFromName()+")");
            viewHolder2.toName.setText("("+model.getToName()+")");
            viewHolder2.goodsType.setText(model.getGoodsType());
            viewHolder2.time.setText(model.getTime());
            viewHolder2.status.setText("【"+model.getStatus()+"】");

            viewHolder2.goodsID2.setText(model.getGoodsID2());
            viewHolder2.fromPlace2.setText(model.getFromPlace2());
            viewHolder2.toPlace2.setText(model.getToPlace2());
            viewHolder2.fromName2.setText("("+model.getFromName2()+")");
            viewHolder2.toName2.setText("("+model.getToName2()+")");
            viewHolder2.goodsType2.setText(model.getGoodsType2());
            viewHolder2.time2.setText(model.getTime2());
            viewHolder2.status2.setText("【"+model.getStatus2()+"】");
        }


        return view;
    }

    class ViewHolder1 {
        TextView goodsID;
        TextView fromPlace;
        TextView toPlace;
        TextView fromName;
        TextView toName;
        TextView goodsType;
        TextView time;
        TextView status;
    }

    class ViewHolder2 {
        TextView goodsID;
        TextView fromPlace;
        TextView toPlace;
        TextView fromName;
        TextView toName;
        TextView goodsType;
        TextView time;
        TextView status;
        TextView goodsID2;
        TextView fromPlace2;
        TextView toPlace2;
        TextView fromName2;
        TextView toName2;
        TextView goodsType2;
        TextView time2;
        TextView status2;
    }

//    public interface onCallDriverAction {
//        void onCallDriverAction(String i);
//    }
//
//    private onCallDriverAction objOnCallDriverAction;
//
//    public void setObjOnCallDriverAction(onCallDriverAction objOnDetailPushListener) {
//        this.objOnCallDriverAction = objOnDetailPushListener;
//    }



}
